package com.quocnv.mediascanner;

import java.util.Observable;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class MediaScannerReceiver extends BroadcastReceiver
{
	private final static String TAG = "MediaScannerReceiver";
	private IMediaStateChangeListener mMediaChangeLis;

	public void setListener(IMediaStateChangeListener listener)
	{
		mMediaChangeLis = listener;
	}

	@Override
	public void onReceive(Context context, Intent intent)
	{
		final String action = intent.getAction();
		final Uri uri = intent.getData();

		String path = uri.getPath();
		String externalStoragePath = Environment.getExternalStorageDirectory()
				.getPath();
		Log.e(TAG, externalStoragePath);

		if (Intent.ACTION_MEDIA_MOUNTED.equals(action))
		{
			Log.e(TAG, "ACTION_MEDIA_MOUNTED");
			Log.e(TAG, path);

			mMediaChangeLis.onStateChange(action, path);
		}
		else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action))
		{
			Log.e(TAG, "ACTION_MEDIA_UNMOUNTED");
			Log.e(TAG, path);

			mMediaChangeLis.onStateChange(action, path);
		}
		else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action))
		{
			Log.e(TAG, "ACTION_MEDIA_SCANNER_FINISHED");
			Log.e(TAG, path);

		}
		else if (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action))
		{
			Log.e(TAG, "ACTION_MEDIA_SCANNER_SCAN_FILE");
			Log.e(TAG, path);
		}
	}
}
